package com.bflow.web.controller;

import com.bflow.api.IBflowDefineActService;
import com.bflow.api.bo.BflowDefineActBo;
import com.bflow.api.bo.BflowDefineActQueryParam;
import com.bflow.api.bo.ext.BflowUserBo;
import com.bflow.api.constant.BflowConstant;
import com.bflow.commons.bean.PageResult;
import com.bflow.commons.bean.TableResult;
import com.bflow.commons.consts.CommConst;
import com.bflow.util.AssertUtil;
import com.bflow.util.JsonUtil;
import com.bflow.web.util.RequestUtil;
import com.bflow.web.util.TestLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Name 活动
 * @Author haowen
 * @Date 2018-12-13
 */
@Controller
@RequestMapping(value = "/bflow/act")
public class BflowActController extends BflowBaseController {

    @Autowired
    private IBflowDefineActService<?, BflowDefineActBo> bflowDefineActService;

    @RequestMapping(value = "list")
    @ResponseBody
    public String list(BflowDefineActQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            PageResult<BflowDefineActBo>  pageResult = bflowDefineActService.findPage(queryParam);
            TableResult result=new TableResult(pageResult.getResult(),pageResult.getTotalCount());
            return JsonUtil.toStr(result);
        } catch (Exception e) {
            return returnFail(e, "列表查询失败");
        }
    }

    @RequestMapping(value = "chooseList")
    @ResponseBody
    public String chooseList(BflowDefineActQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowDefineActBo b=new BflowDefineActBo();
            b.setStatus(BflowConstant.CommStatus.Y.getCode());
            queryParam.setBflowDefineAct(b);
            PageResult<BflowDefineActBo>  pageResult = bflowDefineActService.findPage(queryParam);
            return JsonUtil.toStr(pageResult.getResult());
        } catch (Exception e) {
            return returnFail(e, "列表查询失败");
        }
    }

    @RequestMapping(value = "findByIds")
    @ResponseBody
    public String findByIds(HttpServletRequest req, HttpServletResponse resp) {
        try {
            List<String> ids = RequestUtil.getStrParameterList(req, "ids", ",");
            List<BflowDefineActBo> list = bflowDefineActService.findByIds(ids);
            return returnOk(list, "查询成功");
        } catch (Exception e) {
            return returnFail(e, "查询失败");
        }
    }


    @RequestMapping(value = "get")
    @ResponseBody
    public String get(@RequestParam String id, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowDefineActBo bo = bflowDefineActService.findById(id);
            return returnOk(bo, "查询成功");
        } catch (Exception e) {
            return returnFail(e, "查询失败");
        }
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public String save(BflowDefineActBo bo, HttpServletRequest req, HttpServletResponse resp) {
        try {
            bflowDefineActService.save(bo);
            return returnOk("保存成功");
        } catch (Exception e) {
            return returnFail(e, "保存失败");
        }
    }

    @RequestMapping(value = "del")
    @ResponseBody
    public String del(HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            String id = RequestUtil.getStrParameter(req, "id");
            bflowDefineActService.deleteById(id);
            return returnOk("删除成功");
        } catch (Exception e) {
            return returnFail(e, "删除失败");
        }
    }

    @RequestMapping(value = "editStatus")
    @ResponseBody
    public String editStatus(String id, Boolean flag,HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            BflowDefineActBo bo=new BflowDefineActBo(id);
            bo.setStatus(flag ? CommConst.STATUS_Y : CommConst.STATUS_N);
            bo.setUpdateTime(new Date());
            bflowDefineActService.update(bo);
            return returnOk("更改成功");
        } catch (Exception e) {
            return returnFail(e, "操作失败");
        }
    }



}
